import {
	setWatcher
} from "../../../../../utils/watch";
import Toast from 'tdesign-miniprogram/toast/index';
const app = getApp()
Component({
	options: {
		addGlobalClass: true,
		seleid: true,
		isselected: true,
		listdata: [],
		sum: 0,
		indexnum: 0,
    baseImgUrl: app.globalData.baseImgUrl,
	},

	/**
	 * 组件的属性列表
	 */
	properties: {
		isselected: {
			type: Boolean,
			value: true,
			observer(item) {
				console.log(item);
				this.setData({
					isselected: item
				})
			}
		},
		totalAmount: {
			type: Array,
			value: [],
			observer(item) {
				console.log(item);
				let arr = []
				item.some((item, index) => {
					if (item.selected === true) {
						if (item.inventory > 0) {
							arr.push(item)
						}

					}
				})
				this.setData({
					listdata: arr
				})
				// console.log(arr);
				// let opnum = []
				// this.data.listdata.some((item, index) => {
				// 	opnum.push(item.price)
				// })
				// console.log(opnum);
				// let arrnum = function (nums) {
				// 	return nums.map(Number)
				// }
				// let sum = 0
				// let numdata = arrnum(opnum);

				// function t(value) {
				// 	sum += value;
				// }
				// numdata.forEach(t)
				// console.log(sum);
				// this.setData({
				// 	sum: sum,
				// 	indexnum: arr.length
				// })

				// console.log(arr);
				let add = []
				let vin = wx.getStorageSync('vin')
				let login = wx.getStorageSync('login')
				arr.forEach((r, i) => {
					add.push({
						price: r.price,
						num: r.goodsNum,
						vin: vin||'1234567890',
						productNumber: r.productNumber,
						cartId: r.cartId,
						shippingSpace: r.shippingSpace,
						memberNumber: login.data.data.memberNumber,
						goodsId: r.goodsId
					})
				})
				// console.log(add);
				let listdata = JSON.stringify(add)
				wx.request({
					url: app.globalData.baseApiUrl +'/api/shopping/updateCart',
					method: 'POST',
					data: {
						param: listdata,
						vin: vin||'1234567890',
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
					},
					success: xite => {
						// console.log(xite);
						// console.log(this.data.indexnum);
						this.setData({
							sum: xite.data.data.discountTotalPrices,
						})
						// do something
					},
					fail: err => {}
				})
				// let sum = 0
				// let a = ['1', '2']

				// function t(value) {
				// 	sum += value;
				// }
				// a.forEach(t)
				// console.log(sum);
				// this.setData({
				// 	isselected: item
				// })
			}
		},
		seleid: {
			type: Boolean,
			value: true,
			observer(item) {
				console.log(item);
				this.setData({
					seleid: item
				})
			}
		},
		totalGoodsNum: {
			type: Number,
			value: 0,
			observer(num) {
				console.log(num);
				const isDisabled = num == 0;
				setTimeout(() => {
					this.setData({
						isDisabled,
					});
				});
			},
		},
		totalDiscountAmount: {
			type: Number,
			value: 0,
		},
		bottomHeight: {
			type: Number,
			value: 100,
		},
		numquantity: {
			type: Number,
			value: 0,
			observer(item) {
				console.log(item);
				this.setData({
					sum: item
				})
				// console.log(item);
				// console.log(this.data.listdata);
				// this.data.listdata.forEach((xitem, index) => {
				// 	if (item.goods.cartId === xitem.cartId) {
				// 		if (xitem.selected === true) {
				// 			const arr = item.quantity - 1
				// 			// console.log(arr * xitem.price);
				// 			// console.log(this.data.sum);
				// 			console.log(arr);
				// 			const aun = this.data.sum + arr * xitem.price

				// 			// console.log(xitem.price * item.quantity);
				// 		}
				// 	}
				// })
				// if(item)
			}
		},
		fixed: Boolean,
		numdata: {
			type: String,
			value: '',
			observer(num) {
				console.log(num);
				this.setData({
					indexnum: num
				})
			}
		}
	},
	data: {
		isDisabled: false,
	},

	methods: {
		handleSelectAll() {
			const {
				isselected
			} = this.data;
			console.log(this.data.seleid);
			// if (isselected === false) {
			// 	this.setData({
			// 		isselected: !isselected,
			// 	});
			// 	this.triggerEvent('handleSelectAll', {
			// 		isselected: isselected,
			// 	});
			// } else {
			this.setData({
				isselected: !isselected,
			});
			this.triggerEvent('handleSelectAll', {
				isselected: isselected,
			});
			// }

		},

		handleToSettle() {
			// if (this.data.isDisabled) return;
			if (this.data.listdata.length > 0) {
				this.triggerEvent('handleToSettle');
			} else {
				Toast({
					context: this,
					selector: '#t-toast',
					message: '请先勾选选择商品',
					duration: 2000,
					icon: 'close-circle',
				});
			}

		},
	},
});